<?php
/**
* 名称：邮箱发送类。
* 描述：使用PHP的socket功能，发送邮件。
*/
class Email{
    public $Server=NULL;        #smtp服务器地址
    public $ServerPort=25;      #服务器端口
    public $Username=NULL;      #用户名
    public $Password=NULL;      #密码
    public $IsHTMLEmail=TRUE;   #是否HTML格式内容
    public $Title=NULL;         #标题
    public $Content=NULL;       #内容
    public $Recipients=NULL;    #收件人
    public $IsSuccess=FALSE;    #是否发送成功
    public $Reason=NULL;        #没有发送成功的原因

    public function __Construct($config=NULL){
        if($config){
            foreach($config as $name=>$value){
                $this->$name=$value;
            }
        }
    }

    public function Send(){
        $socket=@fsockopen($this->Server,$this->ServerPort);
        if($socket){
            stream_set_blocking($socket,TRUE);
            $result=fgets($socket);
            //尝试连接
            fputs($socket,"hello \r\n");
            fputs($socket,"AUTH LOGIN \r\n");
            fputs($socket,base64_encode($this->Username)."\r\n");
            fputs($socket,base64_encode($this->Password)."\r\n");
            fputs($socket,"MAIL FROM:<$this->Username>\r\n");
            fputs($socket,"RCPT TO: <$this->Recipients>\r\n");
            fputs($socket,"DATA\r\n");
            //HTML邮件
            if($this->IsHTMLEmail){
                fputs($socket,'Content-type:text/html;'.'\r\n');
            }
            else{
                fputs($socket,'Content-type:text/plain;'.'\r\n');
            }
            //组织邮件内容
            fputs($socket,"From: <$this->Username>\r\n");
            fputs($socket,"Subject: $this->Title\r\n");
            fputs($socket,"To:$this->Recipients\r\n");
            fputs($socket,"\r\n");
            fputs($socket,$this->Content."\r\n");
            fputs($socket,"."."\r\n");
            fputs($socket,"QUIT\r\n");
            fclose($socket);
        }
        else{
            return $this->Reason='服务器链接超时';
        }
    }
}